package cloud.xlh.service.file.service;

import cloud.xlh.domain.file.domain.FileSaveDomain;
import cloud.xlh.domain.file.domain.MountSettingSaveDomain;
import cloud.xlh.domain.file.domain.MountSettingUpdateDomain;
import cloud.xlh.event_pub_and_listen.event.MountSettingEvent;
import cloud.xlh.event_pub_and_listen.publisher.EventPublisher;
import cloud.xlh.infrastructure.file.entity.FileEntity;
import cloud.xlh.infrastructure.file.entity.MountSettingEntity;
import cloud.xlh.infrastructure.file.entity.table.FileEntityTableDef;
import cloud.xlh.infrastructure.file.entity.table.MountSettingEntityTableDef;
import cloud.xlh.infrastructure.file.mapper.FileEntityMapper;
import cloud.xlh.infrastructure.file.mapper.MountSettingMapper;
import cloud.xlh.service.file.adapter.FileServiceAdapter;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/5 11:00
 * @ Description:
 */
@Service
public class FileService {
    @Resource
    private FileServiceAdapter fileServiceAdapter;
    @Resource
    private RemoteFileService remoteFileService;
    @Resource
    private FileEntityMapper fileEntityMapper;
    @Resource
    private MountSettingMapper mountSettingMapper;
    @Resource
    private EventPublisher eventPublisher;

    public String saveFile(FileSaveDomain fileSaveDomain) {
        FileEntity fileEntity = fileServiceAdapter.convertFileEntity(fileSaveDomain);
        int rows = fileEntityMapper.insert(fileEntity);
        if (fileSaveDomain.getRemotePath() != null) {
            remoteFileService.asyncPut(fileSaveDomain.getLocalPath(), fileSaveDomain.getRemotePath());
        } else {
            remoteFileService.asyncPut(fileSaveDomain.getLocalPath());;
        }
        return "000000";
    }

    public String deleteByRemotePath(String remotePath) {
        fileEntityMapper.deleteByQuery(QueryWrapper.create().select()
                .where(FileEntityTableDef.FILE_ENTITY.REMOTE_PATH.eq(remotePath)));
        remoteFileService.asyncRemove(remotePath);
        return "000000";
    }

    public String saveMountSetting(MountSettingSaveDomain mountSettingSaveDomain) {
        MountSettingEntity mountSettingEntity = fileServiceAdapter.convertMountEntity(mountSettingSaveDomain , MountSettingSaveDomain.class);
        mountSettingMapper.insert(mountSettingEntity);
        Map<String, String> eventParam = getEventParam("0"
                , mountSettingSaveDomain.getMpLocalDirPath()
                , mountSettingSaveDomain.getMpRemoteDirPath());
        eventPublisher.publishEvent(new MountSettingEvent(this, "remoteFileService", eventParam));
        return "000000";
    }

    public String deleteByMpPath(String mpLocalDirPath, String mpRemoteDirPath) {
        mountSettingMapper.deleteByQuery(
                QueryWrapper.create().select()
                        .and(MountSettingEntityTableDef.MOUNT_SETTING_ENTITY.MP_LOCAL_DIR_PATH.eq(mpLocalDirPath))
                        .and(MountSettingEntityTableDef.MOUNT_SETTING_ENTITY.MP_REMOTE_DIR_PATH.eq(mpRemoteDirPath)));
        Map<String, String> eventParam = getEventParam("1"
                , mpLocalDirPath
                , mpRemoteDirPath);
        eventPublisher.publishEvent(new MountSettingEvent(this, "remoteFileService", eventParam));
        return "000000";
    }

    public String updateMountSetting(MountSettingUpdateDomain mountSettingUpdateDomain) {
        MountSettingEntity mountSettingEntity = fileServiceAdapter.convertMountEntity(mountSettingUpdateDomain , MountSettingUpdateDomain.class);
        mountSettingMapper.updateByCondition(mountSettingEntity
                , QueryCondition.create(MountSettingEntityTableDef.MOUNT_SETTING_ENTITY.MP_LOCAL_DIR_PATH, mountSettingEntity.getMpLocalDirPath()));
        Map<String, String> eventParam = getEventParam("2"
                , mountSettingUpdateDomain.getMpLocalDirPath()
                , mountSettingUpdateDomain.getMpRemoteDirPath());
        eventPublisher.publishEvent(new MountSettingEvent(this, "remoteFileService", eventParam));
        return "000000";
    }

    private Map<String, String> getEventParam(String settingType , String mpLocalDirPath , String mpRemoteDirPath) {
        Map<String, String> map = new HashMap<>();
        map.put("settingType", settingType);
        map.put("mpLocalDirPath", mpLocalDirPath);
        map.put("mpRemoteDirPath", mpRemoteDirPath);
        return map;
    }

    public List<MountSettingEntity> loadAllMountSetting() {
        return mountSettingMapper.selectAll();
    }

    public String downFile(String localPath, String remotePath) {
        String fileStorePath = remoteFileService.downFile(localPath , remotePath);
        return fileStorePath;
    }
}
